package com.freewater.springboot.demo.dataaccess.jdbc.customtraslator;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator;
import org.springframework.jdbc.support.SQLExceptionTranslator;

import java.util.Optional;

public class Main {
    public static void main(String[] args) {
        final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(AppConfig.class);
        applicationContext.refresh();

        final SomeDao someDao = applicationContext.getBean(SomeDao.class);
        someDao.testCustomSqlError();

        final JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
        final SQLExceptionTranslator exceptionTranslator = jdbcTemplate.getExceptionTranslator();
        System.out.println("Spring JdbcTemplate 默认使用的是 >>> " + exceptionTranslator.getClass());
        final AbstractFallbackSQLExceptionTranslator translator =
                (AbstractFallbackSQLExceptionTranslator) exceptionTranslator;
        System.out.println("自定义的SQLExceptionTranslator >>> " +
                Optional.ofNullable(translator.getCustomTranslator()).map(Object::getClass).orElse(null));
    }
}
